package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.mo.MessageObject;
import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.Customer;
import cn.zj.logistics.pojo.CustomerExample;
import cn.zj.logistics.pojo.Order;
import cn.zj.logistics.pojo.OrderDetail;
import cn.zj.logistics.pojo.OrderExample;
import cn.zj.logistics.pojo.OrderExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.BaseDataService;
import cn.zj.logistics.service.CustomerService;
import cn.zj.logistics.service.OrderService;
import cn.zj.logistics.service.UserService;
import cn.zj.logistics.utils.Constant;

@Controller
@RequestMapping("/order")
public class OrderController {
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private CustomerService customerService;
	
	
	@Autowired
	private BaseDataService baseDataService;
	
	
	//页面跳转功能
	@RequestMapping("/orderPage")
	@RequiresPermissions("order:orderPage")
	public String orderPage() {
		
		return "orderPage";
	}
	
	@RequestMapping("/orderEdit")
	public String orderEdit(Model m,Long orderId) {
		
		/*
		 * 需要共享给新增订单jsp页面的数据
		 * 
		 * 1，业务员
		 * 2，客户
		 * 3，区间管理
		 * 4，付款方式
		 * 5，取件方式
		 * 6，货运方式
		 * 7，单位
		 * 
		 */
		//1，业务员
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRoleNameEqualTo(Constant.ROLE_SALESMAN);
		List<User> users = userService.selectByExample(example );
		//2, 客户
		CustomerExample example1 = new CustomerExample();
		List<Customer> customers = customerService.selectByExample(example1);
		//3，到达区域
		List<BaseData> intervals = baseDataService.selectBaseDataByParentName(Constant.BASIC_COMMON_INTERVAL);	
		//4，付款方式
		List<BaseData> payments = baseDataService.selectBaseDataByParentName(Constant.BASIC_PAYMENT_TYPE);	
		//5，取件方式
		List<BaseData> freights = baseDataService.selectBaseDataByParentName(Constant.BASIC_FREIGHT_TYPE);	
		//6，货运方式
		List<BaseData> fetchTypes = baseDataService.selectBaseDataByParentName(Constant.BASIC_FETCH_TYPE);	
		//7，单位
		List<BaseData> units = baseDataService.selectBaseDataByParentName(Constant.BASIC_UNIT);	
		
		
		m.addAttribute("users", users);
		m.addAttribute("customers", customers);
		m.addAttribute("intervals", intervals);
		m.addAttribute("payments", payments);
		m.addAttribute("freights", freights);
		m.addAttribute("fetchTypes", fetchTypes);
		m.addAttribute("units", units);
		return "orderEdit";
	}
	
	
	
	
	//新增功能
	@RequestMapping("/insert")
	@RequiresPermissions("order:insert")
	@ResponseBody
	public MessageObject insert(@RequestBody Order order) {
		
		System.out.println(order);
		System.out.println(order.getOrderDetails());
		
		MessageObject mo = MessageObject.createMessageObject(0, "新增数据失败，请联系管理员");
		
		int row = orderService.insert(order);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "新增数据成功");
		}
		
		return mo;
	}
	//修改功能
	@RequiresPermissions("order:update")
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(Order order) {
		
		
		MessageObject mo = MessageObject.createMessageObject(0, "修改数据失败，请联系管理员");
		
		int row = orderService.updateByPrimaryKeySelective(order);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "修改数据成功");
		}
		
		return mo;
	}
	
	
	//数据列表功能
	@ResponseBody
	@RequiresPermissions("order:list")
	@RequestMapping("/list")
	public PageInfo<Order> list(@RequestParam(defaultValue = "1") int pageNum,
				@RequestParam(defaultValue = "10") int pageSize,String keyword){
		
		
		PageHelper.startPage(pageNum, pageSize);
		
		OrderExample example = new OrderExample();
		
		//条件搜索查询
		
		if(StringUtils.isNotBlank(keyword)) {
			//发货人模糊查询
			Criteria criteria = example.createCriteria();
			criteria.andShippingNameLike("%"+keyword+"%");
			
		}
		List<Order> orders = orderService.selectByExample(example);

		PageInfo<Order> pageInfo = new PageInfo<Order>(orders);
		return pageInfo;
	}
	
	@RequiresPermissions("order:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long orderId) {
		
		
		//创建消息对象
		MessageObject mo = MessageObject.createMessageObject(0, "删除数据失败，请联系管理员");
		
		int row = orderService.deleteByPrimaryKey(orderId);
		
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "删除数据成功");
		}
		
		return mo;
	}
	/*
	 * 根据订单的id查询出订单详细信息
	 */
	@ResponseBody
	@RequestMapping("/detail")
	public List<OrderDetail> detail(Long orderId){
		
		List<OrderDetail > details =  orderService.selectDetailsByOrderId(orderId);
		
		return details;
	}

	
}
